define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salary/salary_benchmark/index' + location.search,
                    add_url: 'salary/salary_benchmark/add',
                    edit_url: 'salary/salary_benchmark/edit',
                    show_detail_url: 'salary/salary_benchmark/show_detail',
                    del_url: 'salary/salary_benchmark/del',
                    multi_url: 'salary/salary_benchmark/multi',
                    import_url: 'salary/salary_benchmark/import',
                    show_salary_benchmark_log_url: 'salary/salary_benchmark_data/index',
                    table: 'salary_benchmark',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        // {field: 'staff_type', title: __('Staff_type'),searchList: {"1": '普通教师', "2": '非普通教师'},formatter: Table.api.formatter.status,custom:{ 1:'success',2:'warning'}},
                        {field: 'count_type', title: __('Count_type'),searchList: {"1": '次数', "2": '单选'},formatter: Table.api.formatter.status,custom:{ 1:'success',2:'warning'}},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {
                                    name: 'show_detail',
                                    text: '查看详情',
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'salary/salary_benchmark/show_detail',
                                },

                                {
                                    name: 'show_salary_benchmark_log',
                                    text: '查看工资基准项数据',
                                    icon: '',
                                    extend: 'data-area=\'["80%", "80%"]\'',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'salary/salary_benchmark_data/index',
                                },
                                {
                                    name: 'edit',
                                    text: '',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    url: 'salary/salary_benchmark/edit',
                                },
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) { //隐藏自定义的视频按钮
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                if(row.count_type == 2){ //通过Config.chapter 获取后台存的chapter
                                    $(table).data("operate-show_salary_benchmark_log", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        show_detail: function () {
            Controller.api.bindevent();
        },
        show_salary_benchmark_log: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var count_type = $('input[name="row[count_type]"]:checked').val();
                set_display(count_type);
                $('input[name="row[count_type]"]').on('click', function() {
                    // 获取选择的值
                    var count_type = $(this).val();
                    set_display(count_type);
                });

                function set_display(count_type)
                {
                    if(count_type == 1){
                        $(".num_price").css({"display":"block"});
                        $(".staff_type_arr").css({"display":"none"});
                    }else{
                        $(".num_price").css({"display":"none"});
                        $(".staff_type_arr").css({"display":"block"});
                    }
                }
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
